/*
 * @Author:  朱理哲
 * @since: 2019-08-08 16:16:47
 * @lastTime: 2019-08-15 14:57:57
 * @LastEditors: 朱理哲
 */
import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/pages/domain/interest_item.dart';
// import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class ButtonItem extends StatelessWidget {
  final TextStyle textStyle;
  final Color bgStyle;
  final int index;
  final Function onClick;
  ButtonItem({
    Key key,
    @required this.text,
    this.textStyle,
    this.bgStyle,
    @required this.index,
    this.onClick,
  }) : super(key: key);

  final String text;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        boxShadow: [
          //一组阴影
          BoxShadow(
            color: rgba(42, 125, 250, 0.05), //阴影颜色
            offset: Offset(0, 2), //偏移量
            blurRadius: 32, //模糊范围
            spreadRadius: 0, //传播范围
          ),
        ],
      ),
      child: FlatButton(
        child: Text(this.text, style: textStyle),
        color: bgStyle,
        onPressed: onClick,
      ),
    );
  }
}

class InterestContent extends StatefulWidget {
  final Function callBack;
  InterestContent({this.callBack});
  @override
  _InterestContentState createState() => _InterestContentState();
}

class _InterestContentState extends State<InterestContent> {
  @override
  void initState() {
    super.initState();
    this.getPcategory();
  }

  List<int> selectIndexs = []; //选中的兴趣索引
  List<InterestItem> _list = []; //兴趣栏目
  final style = TextStyle(color: rgba(25, 109, 236, 1), fontSize: 14);
  final bgStyle = rgba(233, 242, 254, 1);

  final styleNormal = TextStyle(color: rgba(68, 68, 68, 1), fontSize: 14);
  final bgStyleNormal = rgba(255, 255, 255, 1);
  //更新索引值以及提交表单
  _updateIdxs(index, InterestItem item) {
    if (selectIndexs.indexOf(index) != -1) {
      selectIndexs.remove(index);
    } else {
      selectIndexs.add(index);
    }
    List<InterestItem> selectIds = [];
    selectIndexs.forEach((idx) {
      selectIds.add(_list[idx]);
    });
    widget.callBack(selectIds);
    setState(() {});
  }

  List<Widget> _generateList() {
    return _list
        .asMap()
        .keys
        .map((idx) => ButtonItem(
              text: _list[idx].name,
              textStyle: selectIndexs.indexOf(idx) != -1 ? style : styleNormal,
              bgStyle:
                  selectIndexs.indexOf(idx) != -1 ? bgStyle : bgStyleNormal,
              index: idx,
              onClick: () => {_updateIdxs(idx, _list[idx])},
            ))
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        child: Wrap(
          children: _generateList(),
          spacing: 12,
          runSpacing: 13,
          alignment: WrapAlignment.start,
          runAlignment: WrapAlignment.start,
        ),
        padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      ),
    );
  }

  // 获取索引
  void getPcategory() async {
    var res = await G.req.user.interest();
    if (res.data['status'] == 1) {
      res.data['data']['list']
          .forEach((json) => {_list.add(InterestItem.fromJson(json))});

      setState(() {});
    }
  }
}

class SelectSwiper extends StatelessWidget {
  final int index;
  final double height;
  final Function callBack;
  SelectSwiper({this.callBack, this.index, this.height = 288});
  @override
  Widget build(BuildContext context) {
    return InterestContent(callBack: this.callBack);
  }
}
